<template>
	<view class="content">
		<image class="banner" src="https://onecode-zfx-one.oss-cn-shenzhen.aliyuncs.com/20201111/64c0f5b0-23f1-11eb-9f75-d3178f28c381.png"></image>
		<view class="switch-btn">
			<text class="title">自动拼团</text>
			<u-switch v-model="checked" @change="onSwitchChange" active-color="#4DD865" inactive-color="#eee" size="40"></u-switch>
		</view>
		<view class="rules">
			<view class="title">
				<text class="title-text">玩法规则</text>
				<view class="line"></view>
			</view>
			<view class="text">开启自动参团之后，系统将自动参与新团，直到账户上一次参团的余额不足以参加一个新团。</view>
			<view class="text">两个团之间的差额将原路退回，符合下一个团的资金自动用于下一个团抵扣。</view>
			<view class="text">每个用户每日最多自动参加10次团，达到10次团明日继续自动参团。</view>
		</view>
	</view>
</template>

<script>
	import { mapGetters } from 'vuex';
	export default {
		name: 'auto',
		data() {
			return {
				checked: false,
			}
		},
		async onShow() {
			if (uni.getStorageSync('token')) {
				await this.$store.dispatch('getUserInfo');
				
				if (this.userInfo.isAutoGroup === 1) {
					this.checked = true;
				}
			}
			if (uni.getStorageSync('address') === 'success') {
				uni.removeStorageSync('address');
				// 自动&取消参团
				this.$service.common.user.autoGroup({
					isAutoGroup: 1
				}).then(res => {
					uni.showToast({
						title: '修改成功',
						icon: 'none'
					});
					this.checked = true;
					this.$store.dispatch('getUserInfo');
				})
			}
			// #ifdef MP-WEIXIN
			uni.reportAnalytics('enter_zidongct');
			// #endif
		},
		computed: {
			...mapGetters(['userInfo'])
		},
		methods: {
			// 开启&关闭自动参团
			async onSwitchChange(e) {
				if (!uni.getStorageSync('token')) {
					this.checked = false;
					uni.navigateTo({
						url: '/pages/sign/login?redirectUrl=navigateBack'
					});
					return false;
				}
				if (e) {
					// 校验是否有默认地址
					await this.$service.order.address.defaultAddr().then(res => {
						if (!res) {
							uni.showToast({
								title: '请您先新增一个收货地址',
								icon: 'none'
							});
							this.checked = false;
							
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/member/addressDetail?source=auto'
								})
							}, 1000);
							return false;
						}
						// 自动&取消参团
						this.$service.common.user.autoGroup({
							isAutoGroup: 1
						}).then(res => {
							uni.showToast({
								title: '修改成功',
								icon: 'none'
							});
							this.$store.dispatch('getUserInfo');

							// #ifdef MP-WEIXIN
							uni.reportAnalytics('click_zidongct_cg');
							// #endif
						})
					});
				} else {
					// 自动&取消参团
					this.$service.common.user.autoGroup({
						isAutoGroup: 0
					}).then(res => {
						uni.showToast({
							title: '修改成功',
							icon: 'none'
						});
						this.$store.dispatch('getUserInfo');

						// #ifdef MP-WEIXIN
						uni.reportAnalytics('click_zidongct_sb');
						// #endif
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background: #750B20;
		min-height: 100vh;
		padding-bottom: 20rpx;
		.banner {
			width: 750rpx;
			height: 701rpx;
		}
		.switch-btn {
			padding: 20px;
			background: #fff;
			margin: -80rpx auto 20rpx;
			border-radius: 16rpx;
			color: #333333;
			width: 710rpx;
			position: relative;
			@include flexBox(space-between, center);
			.title {
				font-weight: 600;
				color: #000000;
				font-size: 36rpx;
			}
		}
		.rules {
			padding: 20px;
			background: #fff;
			border-radius: 16rpx;
			color: #333333;
			width: 710rpx;
			margin: 20rpx auto;
			position: relative;
			.title {
				margin-bottom: 20rpx;
				@include flexBox(center, center, column);
				.title-text {
					color: #2B2C2E;
					font-size: 32rpx;
					font-weight: 600;
					z-index: 2;
				}
				.line {
					width: 128rpx;
					height: 16rpx;
					background: $main-color;
					border-radius: 50rpx;
					margin-top: -16rpx;
					z-index: 1;
				}
			}
			.text {
				margin-bottom: 20rpx;
			}
		}
	}
</style>
